package main.helper
{
	import cpnt.iris.RendererIconLabel;
	import cpnt.iris.RendererLabel;
	
	import iris.flex.logic.FlexProcessor;
	
	import main.view.ViewOptional;
	
	import mx.collections.ArrayCollection;
	import mx.controls.advancedDataGridClasses.AdvancedDataGridColumn;
	import mx.controls.advancedDataGridClasses.AdvancedDataGridColumnGroup;
	import mx.core.ClassFactory;
	import mx.events.AdvancedDataGridEvent;
	import mx.events.FlexEvent;
	
	public class ViewOptionalHelper
	{
		public function ViewOptionalHelper()
		{
			if(_instance != null)
				throw new Error("Singleton!");
			_instance = this;
		}
		// 实例
		private static var _instance:ViewOptionalHelper;
		
		public static function getInstance():ViewOptionalHelper
		{
			if(_instance == null)
				new ViewOptionalHelper();
			return _instance;
		}
		// ↑↑↑ Singleton ↑↑↑
		private var _view:ViewOptional;
		
		public function creationCompleteListener(event:FlexEvent):void
		{
			this._view = event.currentTarget as ViewOptional;
			
			_view.dgOptional.addEventListener(AdvancedDataGridEvent.HEADER_RELEASE, dgHeaderRelease);
			var groupedColumns:Array = [];
			groupedColumns.push(
				getColumnGroup("Infomation", 
					[
						getIconLabelColumn("Acquisition", "side", "itemPropertyVo.info_side", "postAcquisition", 240),
						getLabelColumn("iLvl", "itemPropertyVo.info_item_level", 80),
						getIconLabelColumn("ItemName", "items", "itemPropertyVo.info_icon", "itemPropertyVo.info_item_name", 240),
						getLabelColumn("Bind", "itemPropertyVo.info_bind_type", 80),
						getLabelColumn("Unique", "itemPropertyVo.info_unique_type", 80)
					]));
					
			_view.dgOptional.groupedColumns = groupedColumns;
//			<mx:groupedColumns>
//				<mx:AdvancedDataGridColumnGroup headerText="Infomation">
//					<mx:AdvancedDataGridColumn headerText="Acquisition" dataField="postAcquisition" width="240" 
//						dataTipField="postAcquisitionComment" showDataTips="true" />
//					<mx:AdvancedDataGridColumn headerText="iLvl" width="80">
//						<mx:itemRenderer>
//							<mx:Component>
//								<iris:RendererLabel dataFields="itemVo.item_id"/>
//							</mx:Component>
//						</mx:itemRenderer>
//					</mx:AdvancedDataGridColumn>
//					<mx:AdvancedDataGridColumn headerText="ItemName" dataField="infoItemName" width="240" />
//					<mx:AdvancedDataGridColumn headerText="Bind" dataField="lootType" width="80" />
//					<mx:AdvancedDataGridColumn headerText="Unique" dataField="uniqueType" width="80" />
//				</mx:AdvancedDataGridColumnGroup>
//				<mx:AdvancedDataGridColumnGroup headerText="Weapon Stats">
//					<mx:AdvancedDataGridColumn headerText="Dps" dataField="weaponDps" visible="false"/>
//					<mx:AdvancedDataGridColumn headerText="Min" dataField="weaponMinDamage" visible="false" />
//					<mx:AdvancedDataGridColumn headerText="Max" dataField="weaponMaxDamage" visible="false" />
//					<mx:AdvancedDataGridColumn headerText="Speed" dataField="weaponSpeed"/>
//				</mx:AdvancedDataGridColumnGroup>
//				<mx:AdvancedDataGridColumnGroup headerText="AdditionEffect" dataField="postAcquisition">
//				</mx:AdvancedDataGridColumnGroup>
//			</mx:groupedColumns>
		}
		private var _currentDataFields:Array;
		private function dgHeaderRelease(event:AdvancedDataGridEvent):void
		{
			if(event.dataField != null)
				_currentDataFields = event.dataField.split(".");
		}
		private function dgDataProviderUpdate():void
		{
			_view.dgOptional.dispatchEvent(
				new AdvancedDataGridEvent(
					AdvancedDataGridEvent.HEADER_RELEASE, false, false, 1, "itemPropertyVo.info_item_level"));
		}
		private function dgSortCompareFunction(o1:Object, o2:Object):int
		{
			var temp:Object;
			var field:String;
			
			temp = o1;
			for each(field in _currentDataFields)
				temp = temp[field];
			var s1:String = temp as String;
			
			temp = o2;
			for each(field in _currentDataFields)
				temp = temp[field];
			var s2:String = temp as String;
			
			var r:int = s1.localeCompare(s2);
			if(r != 0)
				r = r / Math.abs(r);
			return r;
		}
		private function getLabelColumn(headerText:String, dataFields:String, width:int):AdvancedDataGridColumn
		{
			var column:AdvancedDataGridColumn = getColumn(headerText, dataFields, width);
			
			var factory:ClassFactory = new ClassFactory(RendererLabel);
			factory.properties = {dataFields: dataFields};
			column.itemRenderer = factory;
			
			return column;
		}
		private function getIconLabelColumn(headerText:String, iconDir:String, iconDataFields:String, labelDataFields:String, width:int):AdvancedDataGridColumn
		{
			var column:AdvancedDataGridColumn = getColumn(headerText, labelDataFields, width);

			var factory:ClassFactory = new ClassFactory(RendererIconLabel);
			factory.properties = {iconDir: iconDir, iconDataFields: iconDataFields, labelDataFields: labelDataFields};
			column.itemRenderer = factory;
			
			return column;
		}
		private function getColumn(headerText:String, dataFields:String, width:int):AdvancedDataGridColumn
		{
			var column:AdvancedDataGridColumn = new AdvancedDataGridColumn();
			column.headerText = headerText;
			column.dataField = dataFields;
			column.width = width;
			column.sortCompareFunction = dgSortCompareFunction;
			column.sortDescending = true;
			
			return column;
		}
		private function getColumnGroup(headerText:String, columns:Array):AdvancedDataGridColumnGroup
		{
			var group:AdvancedDataGridColumnGroup = new AdvancedDataGridColumnGroup();
			group.headerText = headerText;
			group.children = columns;
			
			return group;
		}
		
		private var _optionalItemList:ArrayCollection;
		
		public function set optionalItemList(data:ArrayCollection):void
		{
			_optionalItemList = data;
			
			_view.dgOptional.dataProvider = _optionalItemList;
			
			FlexProcessor.extCallLater(dgDataProviderUpdate);
//			_view.pnlOptional.removeAllChildren();
//			
//			for each(var iBean:ItemPropertyBean in list)
//			{
//				var html:HTML = new HTML();
//				html.location = "http://wow.allakhazam.com/ihtml?" + iBean.infoItemId;
//				_view.pnlOptional.addChild(html);
//			}
/*
Weapon Stats	DPS	Min Damage	Max Damage	Speed
Base Stat Bonuses	Strength	Agility	Stamina	Intellect	Spirit
General Bonuses	Crit Rating	Hit Rating	Haste Rating
Physical Combat Bonuses	Attack Power	Armor Penetration	Expertise Rating
Spell Bonuses	Spell Power	Spell Penetration	Mana Regen
Defense Bonuses	Armor	Block Rating	Block Value	Defense Rating	Dodge Rating	Parry Rating	Health Regen	Resilience
Resistance Bonuses	Arcane Resist	Shadow Resist	Nature Resist	Frost Resist	Fire Resist
General Bonuses	Bind on pickup	Bind on equip	Bind on use	Unique	Unique-Equipped
	Has Spell Effect...
*/
		}
//		public function dgOptionalTooltipShown(event:ToolTipEvent):void
//		{
//			var tooltip:IToolTip = event.toolTip;
//			if(tooltip.y + tooltip.height > _view.dgOptional.y + _view.dgOptional.height)
//				tooltip.y = _view.dgOptional.y + _view.dgOptional.height - tooltip.height;
//		}
	}
}